package ml.tewas.weaponassignment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import ml.tewas.commondata.ForceEnum;
import ml.tewas.commondata.ThreatClassificationEnum;
import ml.tewas.commondata.ThreatEvaluationResultStruct;

public class WeaponAssignmentDataService implements IWeaponAssignmentDataService{
	
	
	private static final String QTABLEFILE_PATH = "./ml/tewas/res/QTableStates.txt";;

	public List<TewasSimulationState> readQTableData(){
	
		return readStateList();
	}
	
	private List<TewasSimulationState> readStateList() {

		List<TewasSimulationState> stateList = new ArrayList<TewasSimulationState>();
		BufferedReader br = null;
		String line = null;
		try {
			File f = new File(QTABLEFILE_PATH);
			br = new BufferedReader(new FileReader(f));
			while(br.readLine() != null){
				TewasSimulationState tempState = new TewasSimulationState();
				line = getStringValue(br);
				int threatNumber = Integer.parseInt(line);
				for(int i = 0; i < threatNumber; i++){
					line = getStringValue(br);
					int threatID = Integer.parseInt(line);	
					ThreatEvaluationResultStruct result = new ThreatEvaluationResultStruct();
					line = getStringValue(br);
					result.setAgentOwnClassification(ThreatClassificationEnum.valueOf(line));
					line = getStringValue(br);
					result.putOtherAgentClassifications(ForceEnum.ARMEDAGENT, ThreatClassificationEnum.valueOf(line));
					line = getStringValue(br);
					result.putOtherAgentClassifications(ForceEnum.UNARMED, ThreatClassificationEnum.valueOf(line));
					
					WeaponAssignmentQTableThreatStruct data = new WeaponAssignmentQTableThreatStruct();
					line = getStringValue(br);
					data.setQValue(Float.parseFloat(line));
					line = getStringValue(br);
					data.setTotalQueried(Integer.parseInt(line));
					line = getStringValue(br);
					data.setTotalSelected(Integer.parseInt(line));
					tempState.putEvaluationResult(threatID, result);
					tempState.putQTableThreatData(threatID, data);
				}
				stateList.add(tempState);
			}

		} catch (IOException e) {
			System.out.println(e.getMessage());
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return stateList;
	}

	private String getStringValue(BufferedReader br) {
		String line;
		String result = null;
		try {
			line = br.readLine();
			StringTokenizer tokenizer = new StringTokenizer(line);
			tokenizer.nextToken("	");
			result = tokenizer.nextToken("	");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public void writeQTableData(List<TewasSimulationState> stateList){
		 
		writeStateList(stateList);
	}

	private void writeStateList(List<TewasSimulationState> stateList) {
		BufferedWriter bw = null;
		try {
			File f = new File(QTABLEFILE_PATH);
			bw = new BufferedWriter(new FileWriter(f));
			int index = 1;
			for(TewasSimulationState state : stateList){
				bw.write("__STATE__	"+index+"\n");
				Map<Integer, WeaponAssignmentQTableThreatStruct> qMap = state.getQTableDataMap();
				Map<Integer, ThreatEvaluationResultStruct> evalMap = state.getThreatEvaluationsMap();
				int size = evalMap.size();
				bw.write("NUMBER_OF_THREATS	"+size+"\n");
				Set<Integer> evalKeys = evalMap.keySet();
				for(Integer key : evalKeys){
					bw.write("THREAT_NO	"+key+"\n");
					ThreatEvaluationResultStruct evaluation = evalMap.get(key);
					WeaponAssignmentQTableThreatStruct qTable = qMap.get(key);
					bw.write("OWN_CLASSIFICATION	"+evaluation.getAgentOwnClassification().name()+"\n");
					bw.write("ARMEDAGENT	"+evaluation.getOtherAgentClassification(ForceEnum.ARMEDAGENT).name()+"\n");
					bw.write("UNARMED	"+evaluation.getOtherAgentClassification(ForceEnum.UNARMED).name()+"\n");
					bw.write("qValue	"+qTable.getQValue()+"\n");
					bw.write("totalQueried	"+qTable.getTotalQueried()+"\n");
					bw.write("totalSelected	"+qTable.getTotalSelected()+"\n");
				}
				index++;
			}
			
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	public static void main(String[] args) {
		
		IWeaponAssignmentDataService data = new WeaponAssignmentDataService();
		List<TewasSimulationState> list = data.readQTableData();
		data.writeQTableData(list);
	}
}
